package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Utils.JwtUtils;
import com.itheima.exception.BussinessException;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
  /*  @Override
    public PageBean page(Integer page, Integer pageSize) {
        //1. 获取总记录数
        PageHelper.startPage(page, pageSize);
        //2. 获取结果列表
        List<Emp> empList = empMapper.list();
        Page<Emp> p =(Page<Emp>) empList;
        //3. 封装结果
        return new PageBean(p.getTotal(),p.getResult());
    } */
    @Override
    public PageBean page(EmpQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Emp> empList = empMapper.list(param);
        //解析封装分页结果
        Page<Emp> p =(Page<Emp>) empList;
        return new PageBean(p.getTotal(),p.getResult());
    }
    @Transactional
    @Override
    public void add(Emp emp) {
    //添加员工
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.add(emp);
        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(expr -> expr.setEmpId(empId));
            empExprMapper.insertBatch(exprList);
        }
    }
    @Transactional
    @Override
    public void deleteByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);
        empExprMapper.deleteByIds(ids);
    }
    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }
    @Transactional
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
        empExprMapper.deleteByIds(Arrays.asList(emp.getId()) );//删除旧的
        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();     //插入新的
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(expr -> expr.setEmpId(empId) );
            empExprMapper.insertBatch(exprList);
        }
    }
    @Override
    public List<Emp> pList() { //查询全部
        return empMapper.pList();
    }
    @Override
    public Login login(Emp emp) {
        Emp empLogin = empMapper.login(emp);
        if (empLogin != null) {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("id", empLogin.getId());
            dataMap.put("username", empLogin.getUsername());
            String jwt = JwtUtils.generateJwt(dataMap);
            return new Login(empLogin.getId(), empLogin.getUsername(), empLogin.getName(),jwt);
        }
        return null;
    }
}
